<?php
namespace Personnelsystem\Controller;
use Think\Controller;
use Think\Page;
header("content-type:text/html;charset=utf-8");
class DepartmentController extends LoginTrueController {
    public function Add(){
        $this->LoginTrue();
        $this->LoginAreaAdmin();
        $regione1=session('regione1');
        $regione2=session('regione2');
        $regione3=session('regione3');
        $regione4=session('regione4');
        $this->assign("regione1",$regione1);
        $this->assign("regione2",$regione2);
        $this->assign("regione3",$regione3);
        $this->assign("regione4",$regione4);
        $this->display();
    }
	
    public function getChildId(){//获取行政区代码的子ID
        $this->LoginTrue();
		$pId=$_POST["id"];
		$regione=M("regione");
		$regiones=$regione->where("pId='$pId'")->select();
        
        echo json_encode($regiones);
    }
	
    public function AddAction(){
        $this->LoginAreaAdmin();
        $this->LoginTrue();
        $data["dName"]=$_POST["dName"];
        $data["pId"]=$_POST["regione4"];//  上级地区ID
        if(empty($data["pId"])){
            $this->error("添加失败,请选择所属地区");
        }
        $data["dDirector"]=$_POST["dDirector"];
        if($data["dDirector"]==""){
            $data["dDirector"]="暂未设定负责人";
        }
        $data["dDirectorTel"]=$_POST["dDirectorTel"];
        if($data["dDirectorTel"]==""){
            $data["dDirectorTel"]="暂无完善";
        }
        $data["dDirectorQQ"]=$_POST["dDirectorQQ"];
        if($data["dDirectorQQ"]==""){
            $data["dDirectorQQ"]="暂无完善";
        }
        $data["dDirectorEmail"]=$_POST["dDirectorEmail"];
        if($data["dDirectorEmail"]==""){
            $data["dDirectorEmail"]="暂无完善";
        }
        $data["dInfo"]=$_POST["dInfo"];
        if($data["dInfo"]==""){
            $data["dInfo"]="暂未填写";
        }
        $departmentAdd=M("department");
        $result=$departmentAdd->add($data);
        if($result){
            $this->success("添加成功",U("listsedit"));
        }else{
            $this->error("添加失败");
        }
    }

 
    
    public function ListsEdit(){
        $this->LoginTrue();
        $department=M("department");
        $regione=M("regione");
        $lib=new LibController();

        $regione_list = $regione->select();

        $where=" dId > 0 ";
        if(empty($_POST["regione1"])){
            $regione1=$regione->where("pId='650000000000'")->select();
        }
        $regioneid=$_POST["regione1"];  //地区查询
        if(!empty($_POST["regione2"])){
            $regioneid=$_POST["regione2"];
        }
        if(!empty($_POST["regione3"])){
            $regioneid=$_POST["regione3"];
        }
        if(!empty($_POST["regione4"])){
            $regioneid=$_POST["regione4"];
        }

        if(!empty($regioneid)){     //如果地区查询，且没选择小组

            $lib->relist=$regioneid;
            $lib->createtree($regione_list,$regioneid);
            $where.="and pId in($lib->relist)";


            $lib->getFatherRegione($regione_list,$regioneid);

            $flagFn=$fn=count($lib->father_Regione);
            for($fn ;$fn>0;$fn--){
                if($fn==1){
                    $regione1=session('regione1');
                    //$regione1[]=$lib->father_Regione[$flagFn-1];
                    if($flagFn==1){
                        $pId=$lib->father_Regione[$flagFn-1]['id'];
                        $regione2=$regione->where("pId=$pId")->select();
                    }
                }
                if($fn==2){
                    if(!empty(session('regione2'))){
                        $regione2=session('regione2');
                    }else{
                        $regione2=$regione->where("pId='{$_POST['regione1']}'")->select();
                    }
                    if($flagFn==2){
                        $pId=$lib->father_Regione[$flagFn-2]['id'];
                        $regione3=$regione->where("pId=$pId")->select();
                    }
                }
                if($fn==3){
                    if(!empty(session('regione3'))){
                        $regione3=session('regione3');
                    }else{
                        $regione3=$regione->where("pId='{$_POST['regione2']}'")->select();
                    }
                    if($flagFn==3){
                        $pId=$lib->father_Regione[$flagFn-3]['id'];
                        $regione4=$regione->where("pId=$pId")->select();
                    }
                }
                if($fn==4){
                    if(!empty(session('regione4'))){
                        $regione4=session('regione4');
                    }else{
                        $regione4=$regione->where("pId='{$_POST['regione3']}'")->select();
                    }
                    if($flagFn==4){
                        $pId=$lib->father_Regione[$flagFn-4]['id'];
                        $xiaozulist=$department->field('dId,dName')->where("pId=$pId")->select();
                    }
                }
            }
        }else{
            $regione1=session('regione1');
            $regione2=session('regione2');
            $regione3=session('regione3');
            $regione4=session('regione4');

            $lib->relist=session('manageRegione');
            $lib->createtree($regione_list,session('manageRegione'));
            $where.="and pId in($lib->relist)";
        }

        //处理分页
        if($_POST['page']){
            $pageid=$_POST['page']-1;
            $_GET['p']=$_POST['page'];
        }else
            $pageid=0;
        $count = $department->where($where)->count();// 查询满足要求的总记录数 $map表示查询条件
        $Page = new Page($count,10);// 实例化分页类 传入总记录数  没也显示10条
        $Page->rollPage=7;
        $Page -> setConfig('header','<div class="sum">共%TOTAL_ROW%条</div>');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','尾页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $show = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出

        $rs_department=$department->where($where)->order('dId desc')->select();

		foreach($regione_list as $key => $val){
			$rs_regione_new[$val['id']]=$val['name'];
		}
		foreach($rs_department as $key => $val){
			$rs_department[$key]['pIdname']=$rs_regione_new[$val['pId']];
		}
		
        $this->assign("rs_department",array_slice($rs_department,$pageid*10,10));

        $this->assign("regione1",$regione1);
        $this->assign("regione2",$regione2);
        $this->assign("regione3",$regione3);
        $this->assign("regione4",$regione4);
        $this->assign("regione1id",$_POST['regione1']);
        $this->assign("regione2id",$_POST['regione2']);
        $this->assign("regione3id",$_POST['regione3']);
        $this->assign("regione4id",$_POST['regione4']);
        $this->display();
    }
    public function ListsEditUpdate(){
        $this->LoginAreaAdmin();
        $this->LoginTrue();
        $dId=$_GET["dId"];
        $department=M("department");
        $rs_department=$department->where("dId={$dId}")->find();
        $this->assign("rs_department",$rs_department);
        
        $this->display();
    }
    public function Update(){
        $this->LoginTrue();
        $dId=$_GET["dId"];
        $department=M("department");
        $rs_department=$department->where("dId={$dId}")->find();
        $this->assign("rs_department",$rs_department);
        $this->display();
    }
    public function UpdateAction(){
        $this->LoginAreaAdmin();
        $this->LoginTrue();
        $dId=$_GET["dId"];
        $data["dPid"]=$_POST["dPid"];
        if($data["dPid"]==""){
            $data["dPid"]=0;
        }
        $data["dPsid"]=$_POST["dPsid"];
        if($data["dPsid"]==""){
            $data["dPsid"]=0;
        }
        $data["dName"]=$_POST["dName"];
        $data["dDirector"]=$_POST["dDirector"];
        if($data["dDirector"]==""){
            $data["dDirector"]="暂未设定负责人";
        }
        $data["dDirectorTel"]=$_POST["dDirectorTel"];
        if($data["dDirectorTel"]==""){
            $data["dDirectorTel"]="暂无完善";
        }
        $data["dDirectorQQ"]=$_POST["dDirectorQQ"];
        if($data["dDirectorQQ"]==""){
            $data["dDirectorQQ"]="暂无完善";
        }
        $data["dDirectorEmail"]=$_POST["dDirectorEmail"];
        if($data["dDirectorEmail"]==""){
            $data["dDirectorEmail"]="暂无完善";
        }
        $data["dInfo"]=$_POST["dInfo"];
        if($data["dInfo"]==""){
            $data["dInfo"]="暂未填写";
        }
        $department=M("department");
        $result=$department->where("dId={$dId}")->save($data);
        if($result){
            $this->success("修改成功",U("listsedit"));
        }else{
            $this->error("修改失败");
        }
    }
    public function DelAction(){
        $this->LoginAreaAdmin();
        $this->LoginTrue();
        $dId=$_GET["dId"];
        $staff=M("staff");
        $user=$staff->where("xiaozu={$dId}")->select();
        if(!empty($user)){
            $this->error("该小组下有人员，无法删除，请删除小组下人员后再删除");
        }
        $department=M("department");
        $result=$department->where("dId={$dId}")->delete();
        if($result){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

}